<?php 
class DonHangDAO
{
	public static function ParseToDonHangDTO($row)
	{
		$DonHangDTO = new DonHangDTO();
		$DonHangDTO->dh_ma = $row["dh_ma"];
		$DonHangDTO->dh_ngay_dat = $row["dh_ngay_dat"];
		$DonHangDTO->dh_ngay_giao = $row["dh_ngay_giao"];
		$DonHangDTO->dh_tong_tien = $row["dh_tong_tien"];
		$DonHangDTO->dh_tinh_trang = $row["dh_tinh_trang"];
		$DonHangDTO->dh_username = $row["dh_username"];
		return $DonHangDTO;
	}
	
	public static function DanhSachCacDonHangCuaKhachHang($username)
	{
		$sql = "SELECT * FROM don_hang where dh_username = '$username'";
		$result = QuerySQL($sql);
		$arrayDmDTO = NULL;
		if($result >0)	
		{
			$i = 0;
			while($row = mysql_fetch_array($result))
			{
				$spDTO = DonHangDAO::ParseToDonHangDTO($row);
				$arrayDmDTO[$i] = $spDTO;
				$i = $i + 1;
			}
			StopConnection();
			return $arrayDmDTO;
		}
		return NULL;
	}
	
	public static function ParseToChiTietDonHangDTO($row)
	{
		$ChiTietDonHangDTO = new ChiTietDonHangDTO();
		$ChiTietDonHangDTO->ctdh_ma_don_hang = $row["ctdh_ma_don_hang"];
		$ChiTietDonHangDTO->ctdh_ma_san_pham = $row["ctdh_ma_san_pham"];
		$ChiTietDonHangDTO->ctdh_so_luong = $row["ctdh_so_luong"];
		$ChiTietDonHangDTO->ctdh_don_gia = $row["ctdh_don_gia"];
		$ChiTietDonHangDTO->ctdh_ten_san_pham = $row["sp_ten"];
		return $ChiTietDonHangDTO;
	}
	
	public static function DanhSachDonHang()
	{
		$sql = "SELECT * FROM don_hang";
		$result = QuerySQL($sql);
		$arrayDmDTO = NULL;
		if($result >0)	
		{
		$i = 0;
		while($row = mysql_fetch_array($result))
		{
			$spDTO = DonHangDAO::ParseToDonHangDTO($row);
			$arrayDmDTO[$i] = $spDTO;
			$i = $i + 1;
		}
		StopConnection();
		return $arrayDmDTO;
		}
		return NULL;
	}
	public static function XoaDonHangTheoMaDonHang($MaDonHang)
	{
		$sql = "delete FROM don_hang WHERE dh_ma = '$MaDonHang'";
		$result = QuerySQL($sql);
		//StopConnection();
		if($result >0)
		{
			return true;
		}
		else
		return false;
	}
	
	public static function DanhSachChiTietDonHang($MaDonHang)
	{
		$sql = "SELECT * FROM chi_tiet_don_hang ctdh,san_pham sp WHERE ctdh.ctdh_ma_don_hang = '$MaDonHang' and ctdh.ctdh_ma_san_pham = sp.sp_ma";
		$result = QuerySQL($sql);	
		$i = 0;
		$arrayDmDTO=null;
		if($result >0)
		{
		while($row = mysql_fetch_array($result))
		{
			$spDTO = DonHangDAO::ParseToChiTietDonHangDTO($row);
			$arrayDmDTO[$i] = $spDTO;
			$i = $i + 1;
		}
		StopConnection();
		return $arrayDmDTO;
		}
		return NULL;
	}
	
	public static function CapNhatHangTheoMaDonHang($MaDonHang)
	{
		$sql = "update don_hang set dh_tinh_trang = '1' WHERE dh_ma = '$MaDonHang'";
		$result = QuerySQL($sql);
		//StopConnection();
		if($result >0)
		{
			return true;
		}
		else
		return false;
	}
	
	public static function ThemDonHang($dh)
	{
		$sql="Insert into don_hang(dh_ngay_dat,dh_ngay_giao,dh_tong_tien,dh_tinh_trang,dh_username) values ('$dh->dh_ngay_dat','$dh->dh_ngay_giao','$dh->dh_tong_tien','$dh->dh_tinh_trang','$dh->dh_username')";
		$result = QuerySQL($sql);
		if($result)
		{
			$sql="Select MAX(dh_ma)as ma From don_hang Where dh_username='$dh->dh_username'";
			$result = QuerySQL($sql);
			//echo($sql);
			while($row = mysql_fetch_array($result))
			{
				$ma=$row["ma"];
				return $ma;
			}
			return -1;
		}
		else
		{
			return -1;
		}
	}
	public static function ThemChiTietDonHang($ctdh)
	{
		$sql="Insert into chi_tiet_don_hang (ctdh_ma_don_hang,ctdh_ma_san_pham,ctdh_so_luong, ctdh_don_gia) values('$ctdh->ctdh_ma_don_hang','$ctdh->ctdh_ma_san_pham','$ctdh->ctdh_so_luong','$ctdh->ctdh_don_gia')";
		$result = QuerySQL($sql);
		return $result;
	}
}
?>